package chuishop.yiqimaicai.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import chuishop.yiqimaicai.pojo.Goods;
import chuishop.yiqimaicai.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class GoodsController {

    private static final Logger LOGGER = LoggerFactory.getLogger(GoodsController.class);

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/upload")
    public String upload() {
        return "upload";
    }

    @PostMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }

        String fileName = file.getOriginalFilename();
        String filePath = "D:\\ideaPlace\\chuichui\\yiqimaicai\\images\\goods\\";
        File dest = new File(filePath + fileName);
        try {
            file.transferTo(dest);
            LOGGER.info("上传成功");
            return "上传成功";
        } catch (IOException e) {
            LOGGER.error(e.toString(), e);
        }
        return "上传失败！";
    }

    //查询所有商品
    @RequestMapping("findAllGoods")
    @ResponseBody
    public PageInfo<Goods> findGoodsList(Integer pageNo, Integer pageSize){
        if(pageNo==null){
            pageNo=1;
        }

        if(pageSize==null){
            pageSize = 5;
        }

        //启动分页
        PageHelper.startPage(pageNo,pageSize);

        List<Goods> list = goodsService.findAllGoods();
        PageInfo<Goods> pageInfo = new PageInfo<>(list);

        return pageInfo;
    }

    @RequestMapping("findGoodsInHouse")
    @ResponseBody
    public List<Goods> findGoodsInHouse(){
        List<Goods> list = goodsService.findGoodsInHouse();
        return list;
    }

    @RequestMapping("findGoodsInAB")
    @ResponseBody
    public List<Goods> findGoodsInAB(){
        List<Goods> list = goodsService.findGoodsInAB();
        return list;
    }

    @RequestMapping("findGoodsInGI")
    @ResponseBody
    public List<Goods> findGoodsInGI(){
        List<Goods> list = goodsService.findGoodsInGI();
        return list;
    }

    @RequestMapping("selectGoodsByTyid")
    @ResponseBody
    public List<Goods> selectGoodsByTyid(Integer tyid){
        List<Goods> list = goodsService.selectGoodsByTyid(tyid);
        return list;
    }

    //根据输入查询商品
    @RequestMapping("selectGoodsByName")
    @ResponseBody
    public PageInfo<Goods> selectGoodsByName(Integer pageNo, Integer pageSize, String selectInput){
        if(pageNo==null){
            pageNo=1;
        }

        if(pageSize==null){
            pageSize = 5;
        }

        //启动分页
        PageHelper.startPage(pageNo,pageSize);

        List<Goods> list = goodsService.selectGoodsByName(selectInput);
        PageInfo<Goods> pageInfo = new PageInfo<>(list);

        return pageInfo;
    }

    //添加商品
    @RequestMapping("addGoods")
    @ResponseBody
    public Map<Object, Object> addGoods(@RequestBody Goods goods){

        Map<Object, Object> result = new HashMap<>();

//        System.out.println(address.getAddress()+address.getRemark());
        boolean flag = goodsService.addGoods(goods);
        if(flag){
            result.put("status", "200");
            result.put("msg","添加成功");
        }else {

            result.put("status", "500");
            result.put("msg", "添加失败");
        }
        return result;
    }

    //更新商品
    @RequestMapping("updateGoods")
    @ResponseBody
    public Map<Object, Object> updateGoods(@RequestBody Goods goods){

        Map<Object, Object> result = new HashMap<>();

        boolean flag = goodsService.updateGoods(goods);
        if(flag){
            result.put("status", "200");
            result.put("msg","更新成功");
        }else {

            result.put("status", "500");
            result.put("msg", "更新失败");
        }
        return result;
    }

    //删除商品
    @RequestMapping("delGoods")
    @ResponseBody
    public Map<Object, Object> delGoods(String ids){

        System.out.println(ids);

        String idstr[] = ids.split(",");
        for (String str: idstr){
            System.out.println(str);
        }

        Map<Object, Object> result = new HashMap<>();

        boolean flag = goodsService.deleteGoods(idstr);
        if(flag){
            result.put("status", "200");
            result.put("msg","添加成功");
        }else {

            result.put("status", "500");
            result.put("msg", "添加失败");
        }
        return result;
    }



}
